package com.oristand.printer.address;

import com.oristand.printer.address.model.LabelDetail;
import com.oristand.printer.address.model.PrintRecord;
import com.oristand.printer.address.util.DbUtil;
import com.oristand.printer.address.view.*;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;

/**
 * 入口类
 *
 * @author chenkailiang
 * @date 2020/9/7
 */
public class MainApp extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;
    private File latestUploadFile;

    private ObservableList<LabelDetail> labelDetailList = FXCollections.observableArrayList();
    private ObservableList<PrintRecord> printRecordList = FXCollections.observableArrayList();
    private ObservableList<LabelDetail> selectPrintRecordLabelList = FXCollections.observableArrayList();
    private PrintRecord selectedPrintRecord;

    public  static final String APP_NAME = "DrpPrintPal_1.1";

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        primaryStage.setWidth(1200);
        primaryStage.setHeight(800);
        primaryStage.setResizable(false);
        this.primaryStage.setTitle(APP_NAME);
        this.primaryStage.getIcons().add(new Image("file:resources/images/printer.png"));
        initRootLayout();
        showLabelDetail();
    }

    /**
     * 入口方法
     *
     * @param args 入参
     */
    public static void main(String[] args) {
        launch(args);
    }

    /**
     * 获取基础stage
     *
     * @return stage
     */
    public Stage getPrimaryStage() {
        return primaryStage;
    }

    /**
     * 初始化root页面
     */
    private void initRootLayout() {
        try {
            //更新db文件
            DbUtil.updateDb();
            //加载页面
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/RootLayOut.fxml"));
            rootLayout = loader.load();
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取打开的文件中的label信息
     *
     * @return label列表
     */
    public ObservableList<LabelDetail> getLabelDetailData() {
        return labelDetailList;
    }

    /**
     * 获取打印记录
     *
     * @return 打印记录表
     */
    public ObservableList<PrintRecord> getPrintRecordList() {
        return printRecordList;
    }

    /**
     * 获取最新上传的文件
     *
     * @return 文件
     */
    public File getLatestUploadFile(){
        return latestUploadFile;
    }

    /**
     * 获取指定的打印记录对应打印的label列表
     *
     * @return label列表
     */
    public ObservableList<LabelDetail> getSelectPrintRecordLabelList() {
        return selectPrintRecordLabelList;
    }

    /**
     * 给指定的打印记录对应打印的label列表赋值
     *
     * @param selectPrintRecordLabelList label信息
     */
    public void setSelectPrintRecordLabelList(ObservableList<LabelDetail> selectPrintRecordLabelList) {
        this.selectPrintRecordLabelList = selectPrintRecordLabelList;
    }

    /**
     * 获取指定的打印记录信息
     *
     * @return 打印记录
     */
    public PrintRecord getSelectedPrintRecord(){
        return selectedPrintRecord;
    }

    /**
     * 给指定的打印记录信息赋值
     *
     * @param record 打印记录
     */
    public void setSelectedPrintRecord(PrintRecord record){
         this.selectedPrintRecord = record;
    }


    /**
     * 加载打开的文件内容
     *
     * @param file 打开的文件
     * @param list 文件中label信息
     */
    public void loadLabelDetailFromFile(File file, ObservableList<LabelDetail> list) {
        this.labelDetailList.clear();
        this.labelDetailList.addAll(list);
        this.latestUploadFile = file;
        //展示
        showLabelDetail();
    }

    /**
     * 展示打开的文件的内容
     */
    private void showLabelDetail(){
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/PrintRootLayout.fxml"));
            AnchorPane labelOverview =  loader.load();
            rootLayout.setCenter(labelOverview);
            PrintRootLayoutController controller = loader.getController();
            controller.setMainApp(this);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 展示选中的打印记录中的label信息
     */
    public void showSelectedPrintRecordLabelDialog(){
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/PrintRecordDetail.fxml"));
            AnchorPane page = loader.load();

            Stage dialogStage = new Stage();
            dialogStage.setResizable(false);
            dialogStage.setTitle(this.selectedPrintRecord.getFileName());
            dialogStage.setWidth(900);
            dialogStage.setHeight(500);
            dialogStage.initModality(Modality.WINDOW_MODAL);
            dialogStage.initOwner(primaryStage);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);

            PrintRecordDetailController controller = loader.getController();
            controller.setDialogStage(dialogStage);
            controller.setMainApp(this);
            dialogStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




}
